//
//  CashHomeViewController.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/2.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "CashHomeViewController.h"
#import "AnswersNowTableViewCell.h"
#import "YouHelpPeopleCollectionViewCell.h"
#import "HelpDetailVC.h"
#import "WorkmateMainVC.h"
#import "CertificationViewController.h"
#import "CompanyInfoCertificateVC.h"

@interface CashHomeViewController ()<UITableViewDelegate,UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@property (strong, nonatomic) IBOutlet UIView *headView;

#pragma mark ------我能帮谁
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic)  NSMutableArray *helpArrDate;//我能帮谁数组
@end

@implementation CashHomeViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupNav];
    
    /**加载列表*/
    [self loadTabbleView];
    
    /**加载我能帮谁*/
    [self loadCollectionView];
    
    [self downRefreshRequest];
    [self upRefreshRequest];
}

/** 设置导航栏 */
- (void)setupNav {
    self.navigationItem.title = @"变现";
    self.navigationItem.leftBarButtonItems = nil;
    UIImageView *aImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    aImage.image = [UIImage imageNamed:@"公司icon"];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addSubview:aImage];
    backBtn.frame = CGRectMake(0, 0, 30, 30);
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}

/** 加载列表 */
-(void)loadTabbleView{
//    self.headView.frame = CGRectMake(0, 0, kScreenWidth, 318 + (kScreenWidth - 10)*179/365.0);
    self.headView.frame = CGRectMake(0, 0, kScreenWidth, 381);
    self.tableView.tableHeaderView = self.headView;
    [self.tableView registerNib:[UINib nibWithNibName:@"AnswersNowTableViewCell" bundle:nil] forCellReuseIdentifier:@"AnswersNowTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
}
/** 排行榜CollectionView */
-(void)loadCollectionView{
    [self.collectionView registerNib:[UINib nibWithNibName:@"YouHelpPeopleCollectionViewCell" bundle:nil]  forCellWithReuseIdentifier:@"YouHelpPeopleCollectionViewCell"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.itemSize = CGSizeMake(125,145);
    flowLayout.sectionInset = UIEdgeInsetsMake(0,15, 0, 15);
    self.collectionView.collectionViewLayout = flowLayout;
    self.helpArrDate = [NSMutableArray array];
}

#pragma mark ---- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  11;self.helpArrDate.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"YouHelpPeopleCollectionViewCell";
    YouHelpPeopleCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    WorkmateMainVC *vc = [[WorkmateMainVC alloc] init];
//    CertificationViewController *vc = [[CertificationViewController alloc] init];
    CompanyInfoCertificateVC *vc = [[CompanyInfoCertificateVC alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}



#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;self.arrData.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *identifier = @"AnswersNowTableViewCell";
    AnswersNowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    //    MessageModel *model = self.arrData[indexPath.row];
    //    cell.titleName.text = model.title;
    return cell;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    HelpDetailVC *vc = [[HelpDetailVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        weakSelf.pageNumber = kStartPage;
        
        //        [YJMSendRequest getUserMessageWithPageNo:[NSString stringWithFormat:@"%zd",weakSelf.page] result:^(NSDictionary *result, NSError *error) {
        //            result = [CommonUtils decryptDicWithResult:result];
        //            if([result[@"result"] integerValue] == 1)
        //            {
        //                weakSelf.tableView.emptyDataSetDelegate = weakSelf;
        //                weakSelf.tableView.emptyDataSetSource = weakSelf;
        //
        //                NSArray * listDictionaries = result[@"data"][@"list"];
        //                weakSelf.arrData  = [NSMutableArray array];
        //                for(NSDictionary * listDictionary in listDictionaries){
        //                    MessageModel * listItem = [[MessageModel alloc] initWithDictionary:listDictionary];
        //                    [weakSelf.arrData addObject:listItem];
        //                }
        //
        //
        //                [weakSelf.tableView reloadData];
        //                if (weakSelf.arrData.count < kNumOfPageIntSize) {
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        //
        //                }else{
        [weakSelf.tableView.mj_footer endRefreshing];
        //                }
        [weakSelf.tableView.mj_header endRefreshing];
        //
        //
        //
        //            }else{
        //                [weakSelf.tableView.mj_header endRefreshing];
        //                [weakSelf.tableView.mj_footer endRefreshing];
        //                [CommonUtils showToast:[result objectForKey:@"msg"]];
        //            }
        //        }];
        
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.pageNumber ++;
        
        //        [YJMSendRequest getUserMessageWithPageNo:[NSString stringWithFormat:@"%zd",weakSelf.page]   result:^(NSDictionary *result, NSError *error) {
        //            result = [CommonUtils decryptDicWithResult:result];
        //            if([result[@"result"] integerValue] == 1)
        //            {
        //                weakSelf.tableView.emptyDataSetDelegate = weakSelf;
        //                weakSelf.tableView.emptyDataSetSource = weakSelf;
        //
        //                NSArray * listDictionaries = result[@"data"][@"list"];
        //                for(NSDictionary * listDictionary in listDictionaries){
        //                    MessageModel * listItem = [[MessageModel alloc] initWithDictionary:listDictionary];
        //                    [weakSelf.arrData addObject:listItem];
        //                }
        //                [weakSelf.tableView reloadData];
        //                /**当前数量少于10条时就无下一页*/
        //                if (listDictionaries.count < kNumOfPageIntSize) {
        //                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        //
        //                }else{
        //                    [weakSelf.tableView.mj_footer endRefreshing];
        //                }
        //                [weakSelf.tableView.mj_header endRefreshing];
        //            }else{
        //                [weakSelf.tableView.mj_header endRefreshing];
        //                [weakSelf.tableView.mj_footer endRefreshing];
        //                [CommonUtils showToast:[result objectForKey:@"msg"]];
        //            }
        //        }];
        
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
